
class JsonResponse(object):
    """
    统一响应格式
    """
    def __init__(self, code, msg, data):
        self.code = code
        self.msg = msg
        self.data = data

    @classmethod
    def success(cls, data=None):
        return cls(200, 'success', data)

    @classmethod
    def success_with_msg(cls, msg='成功'):
        return cls(200, msg, None)

    @classmethod
    def error(cls, msg='error'):
        return cls(500, msg, None)

    @classmethod
    def accident_error(cls, data=None):
        return cls(501, 'error', data)

    def to_dict(self):
        return {
            'code': self.code,
            'msg': self.msg,
            'data': self.data
        }